﻿using System;
using ParserGenerator.Input;
using ParserGenerator.SyntacticAnalysis;

namespace ParserGenerator.GrammarConstruction
{
    [Serializable]
    public abstract class GrammarSymbol
    {
        public bool IsTerminal => this is GrammarTerminalSymbol;
        public bool IsNonterminal => this is GrammarNonterminalSymbol;

        public GrammarTerminalSymbol AsTerminal => (GrammarTerminalSymbol)this;
        public GrammarNonterminalSymbol AsNonterminal => (GrammarNonterminalSymbol)this;

        public int Number { get; internal set; }
        public string Name { get; internal set; }
        public SourceContext Context { get; internal set; }
        public TypeExpression Type { get; internal set; }
        public bool IsAutoDefined { get; internal set; }
        public bool HasSimpleName { get; internal set; }
        public bool HasSemanticValue => Type != null && !Type.IsUnit;
        public bool Nullable => IsNonterminal && AsNonterminal.Nullable;

        internal GrammarSymbol()
        {
        }

        public sealed override string ToString()
        {
            return Name;
        }
    }
}
